
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui_internal.h>
# include <imgui.h>
# include <application.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include "ResourceManager.h"
#include "Log.h"

using namespace std;
using json = nlohmann::json;

struct Example :
	public Application
{
	using Application::Application;

	struct Item
	{
		std::string txt, icon,module;
	};

	void OnStart() override
	{
		Log::Init();
		loadJson();
		ResourceManager::Get();
	}

	void OnStop() override
	{
		
	}

	void OnFrame(float deltaTime) override
	{
		auto& io = ImGui::GetIO();

		ImGui::Text("FPS: %.2f (%.2gms)", io.Framerate, io.Framerate ? 1000.0f / io.Framerate : 0.0f);

		ImGui::Separator();

		showMenu();
	}

	void showMenu()
	{
		ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None;
		if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
		{
			for (int i = 0; i < mItems.size(); ++i)
			{
				ImGui::SetNextItemWidth(100);
				if (ImGui::BeginTabItem(mItems[i].c_str()))
				{
					mCurrentIndex = i;
					ImGui::EndTabItem();
				}
			}
			ImGui::EndTabBar();
		}
		const ImVec2 size(50, 50);
		for (auto i : mDatas[mItems[mCurrentIndex]])
		{
			ImVec2 pos = ImGui::GetCursorPos();
			//ImGui::InvisibleButton(i.txt.c_str(), size);
			auto t = ResourceManager::Get().FindTexture(i.module);
			if (t)
			{
				ImGui::Image((void*)t->id, size);
			}
			ImGui::Text(i.txt.c_str());
			//ImGui::SameLine();
			
			//SvgManager::Get().getSvg(i.icon)->draw(pos, size, 0, ImGui::IsItemHovered());
			
		}
	}

	const ImU32 col_base = IM_COL32(0, 0, 0, 255);
	const ImU32 col_hovered = IM_COL32(0, 160, 192, 255);
	void showBtn(const char* title, const char* icon)
	{
		ImGuiWindow* g = ImGui::GetCurrentWindow();
		ImVec2 position = ImGui::GetCursorScreenPos();
		const ImVec2 frame_padding(20, 20);
		const ImVec2 padding(20, 40);
		const ImVec2 size(50, 50);
		int index = 0;
		ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, frame_padding);
		int title_len = std::strlen(title);
		ImVec2 txtSize = ImGui::CalcTextSize(title, title + title_len - 4);
		ImVec2 invisibleButtonSize(size.x, size.y + txtSize.y);
		ImGui::InvisibleButton(title, invisibleButtonSize);
		ImVec2 cur = ImGui::GetCursorPos();
		ImVec2 txt_pos = ImVec2(size.x / 2 - txtSize.x / 2, size.y);
		g->DrawList->AddText(txt_pos, ImGui::IsItemHovered() ? col_hovered : col_base, title, title + title_len - 4);
		
		if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(0))
		{
			
		}
		ImGui::PopStyleVar();
	}

	void loadJson()
	{
		mCurrentIndex = 0;
		std::string path = GetHomePath() + "data/workbench.json";
		std::ifstream fi(path);
		if (fi)
		{
			json doc;
			fi >> doc;
			fi.close();
			for (auto w : doc)
			{
				std::string name = w["name"];
				auto wb = w["workbench"];
				std::string menu = wb["menutxt"];
				std::string icon = wb["icon"];
				for (auto m : wb["modules"])
				{
					for (std::string n : m["icon"]) {
						mDatas[name].emplace_back(Item{ menu,icon,n });
					}
				}
				mItems.emplace_back(name);
			}
		}
	}

	int mCurrentIndex;
	std::vector<std::string> mItems;
	std::map<std::string, std::vector<Item>> mDatas;
};
int main(int argc, char** argv)
{
	Example exampe("Simple", argc, argv);

	if (exampe.Create())
		return exampe.Run();

	return 0;
}